package com.ff.xyh.consumer.dictionary.controller;


import com.ff.xyh.common.entity.result.ResultMessage;
import com.ff.xyh.common.entity.result.ResultObject;
import com.ff.xyh.common.entity.dictionary.JoinerStatus;
import com.ff.xyh.consumer.dictionary.service.JoinerStatusService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 参与用户状态表 前端控制器
 * </p>
 *
 * @author ff
 * @since 2022-10-26
 */
@Api(tags = "参与者状态")
@RestController
@RequestMapping("/dictionary")
public class JoinerStatusController {
    @Autowired
    private JoinerStatusService joinerStatusService;

    @ApiOperation(value = "获取所有参与者状态信息")
    @GetMapping("get-joiner-status-all")
    public ResultObject getAllJoinerStatus() {
        List<JoinerStatus> list = joinerStatusService.list(null);
        if (list == null || list.isEmpty()){
            return ResultObject.error().message(ResultMessage.NO_RESULT);
        } else {
            return ResultObject.ok().data("items", list);
        }
    }

    @ApiOperation(value = "根据id获取参与者状态信息")
    @GetMapping("get-joiner-status-id")
    public ResultObject getJoinerStatusById(
            @RequestParam(name = "id") Integer id) {
        JoinerStatus joinerStatus = joinerStatusService.getById(id);
        if (joinerStatus == null){
            return ResultObject.error().message(ResultMessage.NO_RESULT);
        } else {
            return ResultObject.ok().data("data", joinerStatus);
        }
    }

    @ApiOperation(value = "根据id删除参与者状态信息")
    @GetMapping("delete-joiner-status-id")
    public ResultObject deleteJoinerStatusById(
            @RequestParam(name = "id") Integer id) {
        JoinerStatus joinerStatus = joinerStatusService.getById(id);
        if (joinerStatus == null){
            return ResultObject.error().message(ResultMessage.NO_SUCH_OBJECT);
        } else {
            if (joinerStatusService.removeById(id)) {
                return ResultObject.ok().data("data", joinerStatus);
            } else {
                return ResultObject.error().message(ResultMessage.OBJECT_EXISTS_BUT_FAILED_TO_DELETE);
            }
        }
    }

    @ApiOperation(value = "添加参与者状态信息")
    @PostMapping("insert-joiner-status")
    public ResultObject insertJoinerStatus (
            @RequestBody JoinerStatus joinerStatus) {
        if (joinerStatusService.getById(joinerStatus.getId()) != null){
            return ResultObject.error().message(ResultMessage.OBJECT_ALREADY_EXISTS);
        } else {
            if (joinerStatusService.save(joinerStatus)) {
                return ResultObject.ok().data("data", joinerStatus);
            } else {
                return ResultObject.error().message(ResultMessage.NO_SUCH_OBJECT_BUT_FAILED_TO_INSERT);
            }
        }
    }

    @ApiOperation(value = "修改参与者状态信息")
    @PostMapping("update-joiner-status")
    public ResultObject updateJoinerStatus(
            @RequestBody JoinerStatus joinerStatus) {
        if (joinerStatusService.getById(joinerStatus.getId()) == null){
            return ResultObject.error().message(ResultMessage.NO_SUCH_OBJECT);
        } else {
            if (joinerStatusService.updateById(joinerStatus)) {
                return ResultObject.ok().data("data", joinerStatus);
            } else {
                return ResultObject.error().message(ResultMessage.OBJECT_EXISTS_BUT_FAILED_TO_UPDATE);
            }
        }
    }
}

